﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script src="scripts/Soapi.js" type="text/javascript"></script>

    <script type="text/javascript">
        var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';
        var factory = Soapi.RouteFactory("api.meta.stackoverflow.com", apiKey);
        factory.Users({ pagesize: 100 }).getResponse(function(data)
        {
            // 240 is a safe value for max path length otherwise you risk 400 (Bad Request) errors
            // See http://stackapps.com/questions/889/dev-tip-how-to-batch-up-multiple-id-requests-to-maximize-economy-and-still-avoid/1021#1021

            var idList = new Soapi.Utilities
        .VectorizedIdList(240 - Soapi.Routes.UsersByIdReputationRouteMap.routeFormat.length);
            for (var i = 0; i < data.items.length; i++)
            {
                idList.add(data.items[i].user_id);
            }

            // so now we have a VectorizedIdList loaded with 100 user id values
            var batches = idList.getBatches();

            // batches:            
            //    [0]: [1, 811, 22656, 22164, 23354, 658, 9314, 18393, 98786, 130154, 3043, 2915, 115866, 1288, 12950, 132636, 59303, 1782, 13295, 22459, 136558, 17174, 52443, 16587, 18511, 14048, 130127, 59808, 27414, 132480, 23921, 2598, 13531, 4660, 52738, 1069, 2509]
            //    [1]: [131779, 95589, 893, 33708, 55747, 138112, 146126, 130914, 745, 131932, 139541, 130090, 101371, 140478, 2961, 141911, 132632, 75852, 135200, 1337, 54262, 8435, 76337, 137798, 69307, 3, 56338, 138822, 89334, 4, 91, 133733, 3724, 572, 102937, 47529, 137184, 5190]
            //    [2]: [130109, 74022, 84237, 419, 131713, 42471, 132223, 9345, 44765, 132073, 6309, 14148, 56555, 6258, 16417, 20654, 44330, 16487, 68727, 2, 146719, 133301, 140171, 63550, 130494]
            //    length: 3

            for (var i = 0; i < batches.length; i++)
            {
                // each batch will be a list of user id that will contain the
                // maximum number of items and still remain under the 240 length limit

                factory
                .UsersById(batches[i], { pagesize: batches[i].length })
                .getResponse(function(data)
                {
                    // do something interesting with your batch of users here
                    alert(data.items.length);
                }, function(error)
                {
                    alert(error.message);
                });
            }


        });
    </script>

</head>
<body>
</body>
</html>
